package com.yeban.bams.entity;

import com.yeban.bams.exception.ATMException;
import com.yeban.bams.exception.BalanceNotEnoughException;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/10
 *
 * 1 - 信用账户
 * 可以透支，不允许贷款
 */
public class CreditAccount extends Account {

    private double ceiling; // 透支额度
    private static final long serialVersionUID = 6283167529858572146L;

    public CreditAccount() {
    }

    public CreditAccount(Long id, String password, String name, String personId, String email, double balance, double ceiling) {
        super(id, password, name, personId, email, balance);
        this.ceiling = ceiling;
    }

    public CreditAccount(Long id, String password, String name, String personId, String email, double balance, Integer type, double ceiling) {
        super(id, password, name, personId, email, balance);
        this.ceiling = ceiling;
        this.setType(1);
    }

    /**
     * 重写取款方法
     */
    @Override
    public CreditAccount withdraw(double money) throws BalanceNotEnoughException {
        double balance = getBalance();
        double ceiling = getCeiling();
        double available = balance + ceiling;   // 总共可以使用的金额

        // 超过余额+透支额度，直接失败
        if (money > available) {
            throw new BalanceNotEnoughException("您的账户余额不足！");
        }

        // 卡里余额够支付
        if(money <= balance) {
            setBalance(balance - money);
            return this;
        }

        // 卡里余额不够，需要透支额度
        double overMoney = money - balance;
        if(money <= available) { // 用上透支额度够
            setBalance(0);       // 余额先花完
            setCeiling(ceiling - overMoney);
        }
        return this;
    }

    /**
     * 获取
     * @return ceiling
     */
    public double getCeiling() {
        return ceiling;
    }

    /**
     * 设置
     * @param ceiling
     */
    public void setCeiling(double ceiling) {
        this.ceiling = ceiling;
    }

    @Override
    public String toString() {
        return "CreditAccount{" +
                "ceiling=" + ceiling +
                '}' + super.toString();
    }
}
